//
//  CXClassProjectTableViewCell.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/7.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "CXClassProjectTableViewCell.h"
#import "CXProjectCollectionViewCell.h"
@interface CXClassProjectTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *projectData;


@end
@implementation CXClassProjectTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self getData];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.text = @"专题";
        [self.titleLabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:15]];
        self.titleLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.titleLabel];
        
        self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.moreButton setTitle:@"查看全部 >" forState:UIControlStateNormal];
        [self.moreButton addTarget:self action:@selector(moreButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
        [self.moreButton.titleLabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:12]];
        [self.moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:self.moreButton];
        
        UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
        flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        self.collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLaout];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        self.collection.backgroundColor = [UIColor whiteColor];
        [self.collection registerClass:[CXProjectCollectionViewCell class] forCellWithReuseIdentifier:@"CXProjectCollectionViewCellIdentifier"];
        [self addSubview:self.collection];
    }
    return self;
}

#pragma mark - 网络请求
- (void)getData {
    [CXNetWorkTool getWithUrl:kClassFicationProjectURL parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *array = [NSMutableArray new];
            NSDictionary *dic = result;
            NSDictionary *data = dic[@"data"];
            NSArray *dataSource = data[@"collections"];
            for (NSDictionary *dic in dataSource) {
                CXClassProModel *model = [[CXClassProModel alloc] initWithDictionary:dic];
                [array addObject:model];
            }
            self.projectData = [NSMutableArray arrayWithArray:array];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collection reloadData];
            });
        }
        
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(13, 0, self.width - 100, self.height / 4);
    self.moreButton.frame = CGRectMake(self.titleLabel.right, self.titleLabel.height / 3, 80, self.titleLabel.height - self.titleLabel.height / 3);
    self.collection.frame = CGRectMake(0, self.titleLabel.bottom, self.width, self.height - self.titleLabel.height);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.projectData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CXProjectCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXProjectCollectionViewCellIdentifier" forIndexPath:indexPath];
    item.model = self.projectData[indexPath.item];
    return item;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.width / 2 + 5, self.height - self.titleLabel.height - 15);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CXClassProModel *model = self.projectData[indexPath.item];
    
    self.nextBlock(model.NewId, model.title);
}

#pragma mark - 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return CGFLOAT_MIN;
}


- (void)moreButtonDidPress:(UIButton *)sender {
    self.block();
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
